package com.xxx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoleilu.hutool.convert.Convert;
import com.xiaoleilu.hutool.date.DateTime;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xxx.reggie.common.BaseContext;
import com.xxx.reggie.common.CustomException;
import com.xxx.reggie.common.R;
import com.xxx.reggie.dto.OrdersDto;
import com.xxx.reggie.entity.OrderDetail;
import com.xxx.reggie.entity.Orders;
import com.xxx.reggie.service.AddressBookService;
import com.xxx.reggie.service.OrderDetailService;
import com.xxx.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.DateUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("page->{}",page);
        log.info("pageSize->{}",pageSize);
        log.info("number->{}",number);
        log.info("beginTime->{}",beginTime);
        log.info("endTime->{}",endTime);

        //创建分页查询器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        //创建条件查询器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        DateTime time1 = DateUtil.parse(beginTime);
        DateTime time2 = DateUtil.parse(endTime);
        queryWrapper.eq(number != null,Orders::getNumber,number);
        queryWrapper.orderByAsc(Orders::getOrderTime);
        queryWrapper.between(time1 != null && time2 != null,Orders::getOrderTime,time1,time2);
        ordersService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
//        return null;
    }

    /**
     * 修改订单状态
     * @param map
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Map map){
        log.info("id为->{}",map.get("id").toString());
        log.info("status为->{}",map.get("status").toString());
        //创建id查询器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(map.get("id") != null,Orders::getId,Long.valueOf(map.get("id").toString()));
        if (ordersService.getOne(queryWrapper) == null){
            throw new CustomException("查询订单id错误");
        }else {
            Orders orders = new Orders();
            orders.setStatus(Integer.valueOf(map.get("status").toString()));
            orders.setId(Long.valueOf(map.get("id").toString()));
            ordersService.update(orders,queryWrapper);
        }
        return R.success("状态修改成功!!");
    }

    /**
     *订单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize){
        //创建分页查询器
        Page<Orders> pageInfo = new Page(page,pageSize);
        Page<OrdersDto> pageDto = new Page<>();
        //获取条件查询器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(Orders::getOrderTime);
        ordersService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,pageDto,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            //获取订单id
            Long orderId = item.getId();
            //设置条件查询器
            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId,orderId);
            List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper1);
            if (orderDetails != null){
                String userName = item.getUserName();
                String phone = item.getPhone();
                String address = item.getAddress();
                String consignee = item.getConsignee();
                ordersDto.setAddress(address);
                ordersDto.setPhone(phone);
                ordersDto.setUserName(userName);
                ordersDto.setConsignee(consignee);
                ordersDto.setOrderDetails(orderDetails);
            }
            return ordersDto;
        }).collect(Collectors.toList());
        pageDto.setRecords(list);
        return R.success(pageDto);
    }
}
